package priv.pront.code.national.acwing.search.bfs;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:27
 */
public class Acwing173_矩阵距离 {

    static int n, m;
    static int[][] g;
    static int[][] dist;
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    static Queue<int[]> queue = new LinkedList<>();
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); m = scanner.nextInt();
        g = new int[n][m];
        dist = new int[n][m];
        for(int i = 0; i < n; i++) Arrays.fill(dist[i], -1);

        for(int i = 0; i < n; i++){
            String s = scanner.next();
            for(int j = 0; j < m; j++){
                g[i][j] = (int) s.charAt(j) - 48;
            }
        }

        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(g[i][j] == 1){
                    dist[i][j] = 0;
                    queue.add(new int[]{i, j, 0});
                }
            }
        }

        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            for(int i = 0; i < 3; i++){
                int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
                if(nx < 0 || nx >= n || ny < 0 || ny >= m || dist[nx][ny] != -1) continue;
                dist[nx][ny] = cur[2] + 1;
                queue.add(new int[]{nx, ny, cur[2] + 1});
            }
        }

        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                System.out.print(dist[i][j] + " ");
            }
            System.out.println();
        }

    }
}
